package org.soarsky.wealthchengdu.module.info;

import java.util.ArrayList;
import java.util.List;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.entity.NewsData;
import org.soarsky.wealthchengdu.imageCache.CacheMemUtil;
import org.soarsky.wealthchengdu.imageCache.ImageLoader;
import org.soarsky.wealthchengdu.imageCache.ImageLoader.ImageCallback;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MirrorChengdu_adapter extends BaseAdapter {

	private Context context;
	private List<NewsData> mirror_imgs;

	public MirrorChengdu_adapter(Context context) {
		this.context = context;
		mirror_imgs = new ArrayList<NewsData>();
	}

	public void setList(List<NewsData> mirror_imgs) {
		this.mirror_imgs = mirror_imgs;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mirror_imgs.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		HolderView holderview = null;
		if (convertView == null) {
			holderview = new HolderView();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.cd_mirror_adapter, null);
			holderview.m_imgs = (ImageView) convertView
					.findViewById(R.id.mirror_imgs);
			holderview.m_text = (TextView) convertView
					.findViewById(R.id.mirror_text);

			convertView.setTag(holderview);
		} else {
			holderview = (HolderView) convertView.getTag();
		}

		// 加载图片
		if (MyAppliction.mCn == 0) {
			loadImage(this.mirror_imgs.get(position).getImg(),
					holderview.m_imgs);
			holderview.m_text
					.setText(this.mirror_imgs.get(position).getTitle());
		} else {
			loadImage(this.mirror_imgs.get(position).getImgEn(),
					holderview.m_imgs);
			holderview.m_text.setText(this.mirror_imgs.get(position)
					.getTitleEn());
		}
		return convertView;
	}

	class HolderView {
		ImageView m_imgs;
		TextView m_text;
	}

	// 这个方法还必须写在adapter里面
	private void loadImage(final String url, final ImageView image) {

		// Bitmap bm = CacheMemUtil.getImage(url);
		Bitmap bm = CacheMemUtil.getImageFromCache(url); // 第一步，仅仅只从内存中读取
		if (bm != null) {
			// 包括从本地获取的，未进行图片处理的图片
			// bm = ImageUtil.zoomBitmap(bm, iconWidth, iconHeight);// 缩放处理
			// bm = ImageUtil.getRoundedCornerBitmap(bm, corners);// 圆角处理
			// CacheMemUtil.putImage2Cache(url, bm);

			image.setImageBitmap(bm);
		} else {
			image.setImageResource(R.drawable.mirror_mm);

			ImageLoader.getInstance().loadBitmap(url, new ImageCallback() {

				public void imageLoaded(Bitmap bm) {
					if (bm != null) {
						// //以下两行，根本不该在这里调用，应该在存在本地之前，而不是读取显示时才做处理
						// bm = ImageUtil.zoomBitmap(bm, iconWidth,
						// iconHeight);// 缩放处理
						// bm = ImageUtil.getRoundedCornerBitmap(bm, corners);//
						// 圆角处理
						CacheMemUtil.putImage2Cache(url, bm);
						notifyDataSetChanged();
						// image.setImageBitmap(bm); //不能使用这句方法，否则会在加载完之前错误显示
					}
				}
			});
		}
	}
}
